package com.zero.manage.web.components.query;

import cn.hutool.core.util.StrUtil;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Order;
import com.querydsl.core.types.Predicate;
import com.zero.manage.web.components.entity.QSysLogEntity;
import io.gitee.zerowsh.enums.ToolEnums;
import com.zero.manage.web.components.base.BaseQuery;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import java.util.LinkedHashMap;
import java.util.Objects;

@Getter
@Setter
@ApiModel("系统日志-查询条件")
public class SysLogQuery extends BaseQuery {
    @ApiModelProperty("内容信息")
    private String data;

    @ApiModelProperty("标题")
    private String title;

    @ApiModelProperty("操作")
    private String operation;

    @ApiModelProperty("用户名")
    private String username;

    @ApiModelProperty("请求地址")
    private String requestAddress;

    @ApiModelProperty("等级")
    private ToolEnums.LogLevelEnums level;

    @Override
    public Predicate predicate() {
        QSysLogEntity qSysLogEntity = QSysLogEntity.sysLogEntity;
        Predicate predicate = super.predicate();
        //执行动态条件拼装
        predicate = StrUtil.isBlank(data) ? predicate : ExpressionUtils.and(predicate, qSysLogEntity.data.contains(data));
        predicate = StrUtil.isBlank(title) ? predicate : ExpressionUtils.and(predicate, qSysLogEntity.title.contains(title));
        predicate = StrUtil.isBlank(operation) ? predicate : ExpressionUtils.and(predicate, qSysLogEntity.operation.contains(operation));
        predicate = StrUtil.isBlank(username) ? predicate : ExpressionUtils.and(predicate, qSysLogEntity.username.contains(username));
        predicate = StrUtil.isBlank(requestAddress) ? predicate : ExpressionUtils.and(predicate, qSysLogEntity.requestAddress.contains(requestAddress));
        predicate = Objects.isNull(level) ? predicate : ExpressionUtils.and(predicate, qSysLogEntity.level.eq(level));
        return predicate;
    }


    /**
     * 后端固定排序
     *
     * @return
     */
    @Override
    protected LinkedHashMap<String, String> initSort() {
        LinkedHashMap<String, String> map = new LinkedHashMap<>(1);
        map.put("createTime", Order.DESC.name());
        return map;
    }
}
